package com.wws.family.dialog

import android.app.Activity
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.github.gzuliyujiang.wheelpicker.impl.UnitDateFormatter
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout
import com.wika.basics.base.BaseDialog
import com.wika.basics.widget.TipsToast
import com.wika.framework.manager.AppManager
import com.wws.family.R
import com.wws.family.databinding.DialogAccountBinding
import com.wws.family.utils.DateUtils
import com.wws.family.utils.PublicUtils
import java.text.DecimalFormat

/**
 *@Description: 账本输入框
 *@Date: 2024/7/26 16:04
 *@Author: WangWeiShuo
 */
class AccountInputDialog {
    class Builder(
        context: Activity
    ) : BaseDialog.Builder<Builder>(context) {
        private val mBinding: DialogAccountBinding = DialogAccountBinding.inflate(LayoutInflater.from(context))
        private var onSaveListener: OnSaveListener? = null
        private var accountType = "default"
        private var accountTime: String? = null
        private var selectYear: Int = 2025
        private var selectMonth: Int = 1
        private val decimalFormat = DecimalFormat("00")

        init {
            setContentView(mBinding.root)
            val width = AppManager.getInstance(context).getScreenWidthPx()
            setWidth((width * 0.8).toInt())
            setAnimStyle(BaseDialog.AnimStyle.IOS)
            setGravity(Gravity.CENTER)
        }

        fun showTitle(title: String?): Builder {
            mBinding.tvAccountName.text = title
            return this
        }

        fun showContent(type: Int): Builder{
            if (type == 0){
                mBinding.tvAccountName.text = "新增支出"
                mBinding.rgType.visibility = View.VISIBLE
                mBinding.etAccountName.hint = "请输入支出名称"
                mBinding.etAccountPrice.hint = "请输入支出金额"
            }else{
                mBinding.tvAccountName.text = "新增收入"
                mBinding.rgType.visibility = View.GONE
                mBinding.etAccountName.hint = "请输入收入名称"
                mBinding.etAccountPrice.hint = "请输入收入金额"
            }
            mBinding.btnSure.setOnClickListener {
                val name = mBinding.etAccountName.text.toString()
                val price = mBinding.etAccountPrice.text.toString()

                if(name.isNullOrEmpty()){
                    if(type == 0) {
                        TipsToast.showWarningTips("请输入支出名称")
                    }else{
                        TipsToast.showWarningTips("请输入收入名称")
                    }
                }
                if(accountTime.isNullOrEmpty()){
                    TipsToast.showWarningTips("请选择时间")
                }
                if(price.isNullOrEmpty()){
                    if(type == 0) {
                        TipsToast.showWarningTips("请输入支出金额")
                    }else{
                        TipsToast.showWarningTips("请输入收入金额")
                    }
                }
                dismiss()
                if (onSaveListener != null){
                    onSaveListener?.onSave(name, accountTime!!, price.toDouble(),accountType,selectYear,selectMonth)
                }
            }
            mBinding.btnCancel.setOnClickListener {
                dismiss()
            }
            mBinding.rgType.setOnCheckedChangeListener { group, checkedId ->
                when(checkedId){
                    R.id.rb_y -> {
                        accountType = "Y"
                    }
                    R.id.rb_s -> {
                        accountType = "S"
                    }
                    R.id.rb_z -> {
                        accountType = "Z"
                    }
                    R.id.rb_x -> {
                        accountType = "X"
                    }
                }
            }

            mBinding.tvAccountTime.setOnClickListener {
                selectTime()
            }
            return this
        }

        fun addSaveListener(listener: OnSaveListener): Builder{
            onSaveListener = listener
            return this
        }

        private fun selectTime() {
            PublicUtils.setArrowDownUp(mBinding.ivArrowTime,false)
            val picker = DatePicker(context as Activity)
            val wheelLayout: DateWheelLayout = picker.wheelLayout
            wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
            picker.setOnDatePickedListener { year, month, day ->
                selectYear = year
                selectMonth = month
                accountTime = "$year-${decimalFormat.format(month)}-${decimalFormat.format(day)}"
                val week = DateUtils.obtainWeek(accountTime,DateUtils.YYYYMMDD)
                mBinding.tvAccountTime.text = accountTime + "(${week})"
            }
            picker.setOnDismissListener {
                PublicUtils.setArrowDownUp(mBinding.ivArrowTime,true)
            }
            wheelLayout.setDateFormatter(UnitDateFormatter())
            wheelLayout.setRange(DateEntity.target(2020, 1, 1), DateEntity.today(), DateEntity.today())
            picker.wheelLayout.setResetWhenLinkage(false)
            picker.show()
        }
    }

    interface OnSaveListener{
        fun onSave(name: String,time: String,price:Double,type:String,year: Int,month: Int)
    }
}